var supplier_product_caption = '產品細項:';
var supplier_product_murl = '/store/rpc?q=supplier';
var supplier_product_durl = '/store/rpc?q=product&sid=';

jQuery("#dbm_supprod_mas").jqGrid({
   	url:supplier_product_murl,
	height: 130,
	datatype: "json",
   	colNames:['編號','廠商名稱', '地址', '聯絡電話','電子郵件','註記'],
   	colModel:[
   		{name:'id',index:'__key__', width:55,editable:false,editoptions:{readonly:true,size:10}},
   		{name:'name',index:'name', width:90,editable:true,editoptions:{size:25}},
   		{name:'postal_addr',index:'postal_addr', width:90,editable:true,editoptions:{size:25}},
   		{name:'contact_phone',index:'contact_phone', width:90,editable:true,editoptions:{size:25}},
   		{name:'contact_email',index:'contact_email', width:90,editable:true,editoptions:{size:25}},
   		{name:'note',index:'note', width:100, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"20"}}
   	],
   	rowNum:5,
   	rowList:[5,10,20],
   	imgpath: gridimgpath,
   	pager: jQuery('#dbm_supprod_mas_page'),
    viewrecords: true,
    sortorder: "asc",
    jsonReader: {
      repeatitems : false,
      id: "0"
    },
    multiselect: false,
    caption:"供應商清單",
    onSelectRow: function(ids) {
      if(ids == null) {
        ids=0;
        if(jQuery("#dbm_supprod_slav").getGridParam('records') >0 )
        {
          jQuery("#dbm_supprod_slav").setGridParam({url:"/store/rpc?q=product&sid="+ids,page:1})
          .setCaption(supplier_product_caption+ids)
          .trigger('reloadGrid');
        }
      } else {
        row = jQuery("#dbm_supprod_mas").getRowData(ids);
        jQuery("#dbm_supprod_slav").setGridParam({url:"/store/rpc?q=product&sid="+ids,page:1})
        .setCaption('廠商"'+ row.name + '" 的' + supplier_product_caption)
        .trigger('reloadGrid');			
      }
    }
}).navGrid('#dbm_supprod_mas_page',{add:false,edit:false,del:false, search:false});
jQuery("#dbm_supprod_slav").jqGrid({
	height: 200,
  url: supplier_product_durl+'1',
	datatype: "json",
  colNames:['ID','產品名','規格編號'
    ,'品牌', '規格','Tag'
    ,'進貨價','參考價','存量','安全存量', '註記'],
  colModel:[
    {name:'id',index:'__key__', width:55},
    {name:'name',index:'name', width:90,editable:true,editoptions:{size:25}},
    {name:'product_sn',index:'product_sn', width:90,editable:true,editoptions:{size:25}},
    {name:'brand',index:'brand', width:90,editable:true,editoptions:{size:25}},
    {name:'spec',index:'spec', width:90,editable:true,editoptions:{size:25}},
    {name:'tag',index:'tag', width:90,editable:true,editoptions:{size:25}},
    {name:'purchase_price',index:'purchase_price', width:80,editable:true, align:"right",editoptions:{size:25}},
    {name:'ref_price',index:'ref_price', width:80,editable:true, align:"right",editoptions:{size:25}},
    {name:'stock_quantity',index:'stock_quantity', width:80,editable:true, align:"right",editoptions:{size:25}},
    {name:'safe_quantity',index:'safe_quantity', width:80,editable:true, align:"right",editoptions:{size:25}},
    {name:'note',index:'note', width:100, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"20"}}
  ],

  rowNum:10,
  rowList:[10,20,30],
  imgpath: gridimgpath,
  pager: jQuery('#dbm_supprod_slav_page'),
  viewrecords: true,
  sortorder: "asc",
  multiselect: false,
  caption:supplier_product_caption,
  editurl:"/store/product",
  jsonReader: {
    repeatitems : false,
    id: "0"
  }
}).navGrid('#dbm_supprod_slav_page',
  {add:true,edit:true,del:true, search:false}, //bar option
  {height:280,reloadAfterSubmit:false}, // edit options
  {height:280,reloadAfterSubmit:false// add options
    , beforeSubmit : function(postdata, formid) {
      sid = jQuery("#dbm_supprod_mas").getGridParam('selrow');
      postdata.sid = sid;
      return [true, "OK", ""];
    }
    , afterSubmit : function(response, postdata) {
      eval('res = ' + response.responseText);
      //jQuery('#dbm_supprod_slav').addRowData(res.id, res);
      return [true, "OK", res.id];
    }
  }, // add options
  {reloadAfterSubmit:false}, // del options
  {} // search options
);
